﻿Ext.define("PSI.BaseCode.Runtime.BaseCodeInfoEdit", {

    config: {
        fid: "",
        name: "",
        reid: "",
        colModel: ""
    },

    constructor: function (config) {
        this.initConfig(config);
    },

    createUI: function (data) {

        var me = this;
        var colmodel = me.getColModel();
        var dynamicWidth=0;
        var tempNum = 0;
        
        var formArry = [{
            xtype: "hidden",
            name: "bc_id",
            value: data == null ? "" : data.bc_id
        }]
        for (var i = 0; i <colmodel.length; i++) {
            if (colmodel[i].isVisible == 1)
            {
                formArry.push(me.createForm(colmodel[i],data));
                tempNum++;

            }
        }

        dynamicWidth = tempNum * 50;

     

        var AddMetadatawindow = Ext.create("Ext.window.Window", {
            title:data==null? "新增"+me.getName():"编辑"+me.getName(),
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: dynamicWidth,
            border: false,
            layout: "fit",
            items: [
                 {
                     id: "editForm",
                     xtype: "form",
                     layout: "vbox",
                     height: "100%",
                     width: 300,
                     bodyPadding: 5,
                     defaultType: 'textfield',
                     defaults: { // defaults are applied to items, not the container
                         margin: "10 0 0 0",
                         width: 370
                     },
                     bodyStyle: {
                         // background: '#ffc',
                         padding: '10px'
                     },
                     fieldDefaults: {
                         labelWidth: 60,
                         labelAlign: "right",
                         labelSeparator: "",
                         msgTarget: 'side'
                     },
                     items: formArry,
                     buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: function () { this.createInfo() },
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.addtablewindow.close();
                                });
                            }, scope: this
                        }
                     ]
                 }

            ]

        }).show();

        me.addtablewindow = AddMetadatawindow;

    },

    show: function (data) {

        return this.createUI(data);

    },

    createInfo: function (id) {
        var me = this;
        var f = Ext.getCmp("editForm");

        var grid = Ext.getCmp("bcgrid");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/BaseCodeRuntime/AddBCInfo",
            method: "POST",
            params: {
                basecodeid:me.getReid() 
            },
            success: function (form, action) {
                el.unmask();
                var cc = action.result.success;
                if (cc) {
                    PSI.MsgBox.showInfo("数据保存成功", function () {
                        me.addtablewindow.close();

                        var stroe = grid.getStore();

                      //  var id = (action.result.id).toUpperCase();

                        stroe.load();
                        
                    });
                }
                else {
                    Ext.Msg.alert('错误提示', action.result.msg);
                }
            },
            failure: function (form, action) {
                el.unmask();
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('Failure', 'ajax链接失败!');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', action.result.msg);
                }
            }
        });
    },

    createForm:function(model,data){
        
        var formArray={}
        
        switch (model.columnType) {
            case "varchar":
                formArray = {
                    fieldLabel: model.columnTitle,
                    name: model.columnName,
                    value: data == null ? "" : data[model.columnName]
                }
                if (model.columnTitle == "码表名")
                {
                    formArray.allowBlank = false;
                    formArray.blankText == "没有输入名称";
                    formArray.beforeLabelTextTpl = PSI.Const.REQUIRED;
                }
                break;
            case "datetime":

                var value=data[model.columnName];

                var dt = value.replace("T", " ");

                var ff = Date.parse(dt.replace(/-/g, "/"));

                var newValue = new Date(ff);

                var time = Ext.Date.format(newValue, 'Y-m-d H:m');

                var tempstr = time.substr(0, 4);

                if (tempstr == "1901" || tempstr == "1900") {
                    value = "";
                }
                else {

                   value= time;
                }
                formArray = {
                    fieldLabel: model.columnTitle,
                    xtype: "datefield",
                    name: model.columnName,
                    format: "Y-m-d H:m",
                    value: data == null ? "" : value,
                }
                break;
            case "int":
                formArray = {
                    fieldLabel: model.columnTitle,
                    xtype: "numberfield",
                    name: model.columnName,
                    value: data == null ? "" : data[model.columnName],
                }
                break;
            case "float":
                formArray = {
                    fieldLabel: model.columnTitle,
                    xtype: "numberfield",
                    name: model.columnName,
                    value: data == null ? "" : data[model.columnName],
                }
                break;
        
        }

        return formArray
    }
})